﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using YellowMap.Resources;
using System.Threading.Tasks;
using YellowMap.View.Control;

namespace YellowMap.View.Page
{
    public partial class FavoritesPage : PhoneApplicationPage
    {
        public static FavoritesPage Intance { get; set; }
       
        /*
       * [vttrung]: FT05
       */
        public FavoritesPage()
        {
            InitializeComponent();
            ReloadLanguage(AppViewModel.Instance.SettingVM.Language);
            Intance = this;
            Init("");
            LoadTheme();
            
        }

   

        // Detect Theme of Favorites screen and Load theme to app
        private void LoadTheme()
        {
            TitFvorites.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            (ApplicationBar as ApplicationBar).BackgroundColor = AppViewModel.Instance.SettingVM.BGColor_Appbar;
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] != Visibility.Visible)
            {
                //ImgBrush.ImageSource = null;
                LayoutRoot.Background = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_screen);
            }
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            //base.OnBackKeyPress(e);
            SearchPage.isFromSearchPage = false;
        } 

        public static Favorite DetetedFavorite { get; set; }
        // Delete one Item
        public void DeleteItem()
        {
            Favorite row = DetetedFavorite;
            Delete(row);
        }

        //Handle event delete
        async void Delete(Favorite row)
        {
            await DBHelper.DeleteFavouritesItem(row);
            Init("");
        }

        // Load Language when change language in settings screen
        private void ReloadLanguage(string languageName)
        {
            switch (languageName)
            {
                case "Vi":

                    (ApplicationBar.Buttons[0] as ApplicationBarIconButton).Text = LangVi.str_deleteall;
                    TitFvorites.Text = LangVi.str_favorites;
                    break;
                case "En":
                   
                    (ApplicationBar.Buttons[0] as ApplicationBarIconButton).Text = LangEn.str_deleteall;
                    TitFvorites.Text = LangEn.str_favorites;
                    break;
            }
        }

        // set date source for 6 list boxes and handle enabled or disable application bar icon and menu
        async void Init(string id)
        {
            await UpdateDB(id);
            lbfavorites.ItemsSource = AppViewModel.Instance.FavoriteVM.Favorites;
            if (lbfavorites.Items.Count > 0)
                (ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = true;
            else
                (ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = false;
        }

        // Update data base if have change
        async Task UpdateDB(string id = "")
        {
            await AppViewModel.Instance.FavoriteVM.Update();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ReloadLanguage(AppViewModel.Instance.SettingVM.Language);
            base.OnNavigatedTo(e);
        }

        //Handle load pivot index 
        //private void pivotFavorites_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    Init(pivotFavorites.SelectedIndex);
        //}

        // Handle event of View button
        private void btnView_Click(object sender, EventArgs e)
        {
            //switch (pivotFavorites.SelectedIndex)
            //{
            //    case 0:
            //        MainPage.SelectedFavorite = lbfoodDrink.SelectedItem as Favorite;
            //        break;
            //    case 1:
            //        MainPage.SelectedFavorite = lbtravel.SelectedItem as Favorite;
            //        break;
            //    case 2:
            //        MainPage.SelectedFavorite = lbentertainment.SelectedItem as Favorite;
            //        break;
            //    case 3:
            //        MainPage.SelectedFavorite = lbeducation.SelectedItem as Favorite;
            //        break;
            //    case 4:
            //        MainPage.SelectedFavorite = lbshopping.SelectedItem as Favorite;
            //        break;
            //    case 5:
            //        MainPage.SelectedFavorite = lbservice.SelectedItem as Favorite;
            //        break;

            //}
            AppViewModel.Instance.SettingVM.SelectedFavorite = FavoriteListItem.PressedItem.DataContext as Favorite;
            //MainPage.SelectedFavorite = lbfavorites.SelectedItem as Favorite;
            MainPage.COMMAND = MainPage_Command.SHOW_FAVORITE;
            if (MainPage.Instance.tbSearch.Visibility == System.Windows.Visibility.Visible)
            {
                MainPage.Instance.tbSearch.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.titleBG.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.imagesearch.Visibility = System.Windows.Visibility.Collapsed;
            }
            if ((MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled == false)
            {
                (MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled = true;
            }
            NavigationService.GoBack();
        }

        // Handle event of Search button
        private void btnSearch_Click(object sender, EventArgs e)
        {
            //switch (pivotFavorites.SelectedIndex)
            //{
            //    case 0:
            //        MainPage.SelectedFavorite = lbfoodDrink.SelectedItem as Favorite;
            //        break;
            //    case 1:
            //        MainPage.SelectedFavorite = lbtravel.SelectedItem as Favorite;
            //        break;
            //    case 2:
            //        MainPage.SelectedFavorite = lbentertainment.SelectedItem as Favorite;
            //        break;
            //    case 3:
            //        MainPage.SelectedFavorite = lbeducation.SelectedItem as Favorite;
            //        break;
            //    case 4:
            //        MainPage.SelectedFavorite = lbshopping.SelectedItem as Favorite;
            //        break;
            //    case 5:
            //        MainPage.SelectedFavorite = lbservice.SelectedItem as Favorite;
            //        break;

            //}

            //[pkhuy]
            if (AppViewModel.Instance.SettingVM.SearchFrom == Constant.SEARCHFROM_CHOOSEONMAP)
            {
                AppViewModel.Instance.SettingVM.MapLattitude = MainPage.Instance.CenterLocation.Latitude;
                AppViewModel.Instance.SettingVM.MapLongitude = MainPage.Instance.CenterLocation.Longitude;
            }

            AppViewModel.Instance.SettingVM.SelectedFavorite = FavoriteListItem.PressedItem.DataContext as Favorite;
            MainPage.COMMAND = MainPage_Command.SEARCH_FAVORITE;
            if (MainPage.Instance.tbSearch.Visibility == System.Windows.Visibility.Visible)
            {
                MainPage.Instance.tbSearch.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.titleBG.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.imagesearch.Visibility = System.Windows.Visibility.Collapsed;
            }
            if ((MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled == false)
            {
                (MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled = true;
            }
            NavigationService.GoBack();
        }

        //Handle delete all list favoriest
        private void DeleteAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lbfavorites.Items.Count; i++)
            {
                Favorite value = lbfavorites.Items[i] as Favorite;
                Delete(value);
            }
            //switch (pivotFavorites.SelectedIndex)
            //{
            //    case 0:
            //        for (int i = 0; i < lbfoodDrink.Items.Count; i++)
            //        {
            //            Favorite value = lbfoodDrink.Items[i] as Favorite;
            //            Delete(value);
            //        }
            //        break;
            //    case 1:
            //        for (int i = 0; i < lbtravel.Items.Count; i++)
            //        {
            //            Favorite value = lbtravel.Items[i] as Favorite;
            //            Delete(value);
            //        }
            //        break;
            //    case 2:
            //        for (int i = 0; i < lbentertainment.Items.Count; i++)
            //        {
            //            Favorite value = lbentertainment.Items[i] as Favorite;
            //            Delete(value);
            //        }
            //        break;
            //    case 3:
            //        for (int i = 0; i < lbeducation.Items.Count; i++)
            //        {
            //            Favorite value = lbeducation.Items[i] as Favorite;
            //            Delete(value);
            //        }
            //        break;
            //    case 4:
            //        for (int i = 0; i < lbshopping.Items.Count; i++)
            //        {
            //            Favorite value = lbshopping.Items[i] as Favorite;
            //            Delete(value);
            //        }
            //        break;
            //    case 5:
            //        for (int i = 0; i < lbservice.Items.Count; i++)
            //        {
            //            Favorite value = lbservice.Items[i] as Favorite;
            //            Delete(value);
            //        }
            //        break;
            //}
            //Init(pivotFavorites.SelectedIndex);
        }

        private void btnDelete_Click_1(object sender, RoutedEventArgs e)
        {
            DetetedFavorite = FavoriteListItem.PressedItem.DataContext as Favorite;
            DeleteItem();
        }
        //Handle when listbox when user choose item to disalbe or enable View and search button
    }
}